package loja.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import loja.model.pessoa.cliente.Cliente;
import loja.services.factory.ServiceFactory;
import loja.services.factory.helper.ServiceFactoryHelper;

@FacesConverter(forClass = Cliente.class)
public class ClienteConverter implements Converter {

	private ServiceFactory serviceFactory = ServiceFactoryHelper
			.getServiceFactory();

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1,
			String stringCliente) {

		if ((stringCliente == null) || (stringCliente.trim().length() == 0))
			return null;
		Long idCliente = Long.valueOf(stringCliente);
		return this.serviceFactory.getClienteService().consultarClientePorId(
				idCliente);

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1,
			Object objetoCliente) {
		if (objetoCliente == null)
			return null;
		Cliente cliente = (Cliente) objetoCliente;
		return String.valueOf(cliente.getIdPessoa().longValue());
	}

}
